package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bo.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;
import java.lang.System;
import java.util.ArrayList;
import java.util.List;


@Service
public class GoodsServiceImpl implements  GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    Goods_attributeMapper goods_attributeMapper;
    @Autowired
    Goods_specificationMapper goods_specificationMapper;
    @Autowired
    Goods_productMapper goods_productMapper;
    @Override
    public BaseListData list(Integer page, Integer limit, String sort, String order)
    {

        PageHelper.startPage(page,limit);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort+" "+order);
       // List<GoodsBO> goods1 = goodsMapper.selectByGoodsSnAndName(goodsSn,name);
        // System.out.println(goods1);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goods);
        long total = pageInfo.getTotal();
        return BaseListData.ok(goods,total);
    }

    @Override
    public GoodsEdit detail(Integer id) throws JsonProcessingException {
        GoodsEdit goodsEdit = new GoodsEdit();
        List<String> category = new ArrayList();
        String categoryPid = categoryMapper.selectPidfromCategory(id);
        String categoryId = categoryMapper.selectidfromCategory(id);
        category.add(categoryPid);
        category.add(categoryId);
        //System.out.println(category);
        goodsEdit.setCategoryIds(category);
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        goodsEdit.setGoods(goods);
        List<Goods_attribute>  goods_attribute = goods_attributeMapper.selectByGoodsId(id);
        //System.out.println(goods_attribute);
        goodsEdit.setAttributes(goods_attribute);
        List<Goods_specification>  goods_specifications = goods_specificationMapper.selectSpecificationsByGoodsId(id);
        //System.out.println(goods_specifications);
        goodsEdit.setSpecifications(goods_specifications);
        List<Goods_product> goods_product = goods_productMapper.selectProductByGoodsId(id);
        //System.out.println(goods_product);
        goodsEdit.setProducts(goods_product);
        return goodsEdit;
    }

    @Override
    public int delete(Goods goods) {
        int id = goodsMapper.deleteByPrimaryKey(goods.getId());
        if(id == 1){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public BaseListData list2(Integer page, Integer limit, String sort, String order, Integer goodsSn, String name) {
        PageHelper.startPage(page,limit);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort+" "+order);
        List<Goods> goods1 = goodsMapper.selectByGoodsSnAndName(goodsSn,name);
        //logger.info(goods1);
        System.out.println(goods1);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
//        for(int i =0;i<goods.size();i++){
//            if(goods1.contains(goods.get(i))){
//                continue;
//            }else{
//                goods.remove(i);
//            }
//        }
        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goods1);
        long total = pageInfo.getTotal();
        return BaseListData.ok(goods1,total);
    }

    @Override
    public int create(Goods_attribute goods_attribute,
                      GoodsBO goodsBO,
                      Goods_product goods_product,
                      Goods_specification goods_specification) {
//        List<Goods_attributeBO> goods_attributeBOS = shangJiaBO.getGoods_attributeBOS();
//        List<Goods_productBO> goods_productBOS = shangJiaBO.getGoods_productBOS();
//        List<Goods_specificationBO>goods_specificationBOS = shangJiaBO.getGoods_specificationBOS();
        Goods goods = new Goods();
//        goods.setBrandId(goodsBO.getBrandId());
//        goods.setCategoryId(goodsBO.getCategoryId());
//        goods.setBrandId(goodsBO.getBrandId());
//        goods.setCounterPrice(goodsBO.getCounterPrice());
//        goods.setDetail(goodsBO.getDetail());
//        ObjectMapper objectMapper = new ObjectMapper();
//        String content = goodsBO.getGallery();
//        List<String> list = objectMapper.readValue(content,List.class)
//        goods.setGallery();
//        goods.setGoodsSn(goodsBO.getGoodsSn());
//        goods.setIsHot();
//        goods.setIsNew();
//        goods.setIsOnSale();
//        goods.setKeywords(goodsBO.getKeywords());
//        goods.setName(goodsBO.getName());
//        goods.setPicUrl(goodsBO.getPicUrl());
//        goods.setRetailPrice(goodsBO.getRetailPrice());
//        goods.setUnit(goodsBO.getUnit());
//        goodsMapper.insert(goods);

        int res = goodsMapper.insertSelective(goods);
        if(res == 1){
            return 1;
        }else{
            return 0;
        }
    }
}
